package renguanyu.app.codebuilder.view.createcode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import renguanyu.app.codebuilder.model.Template;
import renguanyu.app.codebuilder.util.DaoFactory;
import renguanyu.app.codebuilder.util.RegexUtil;

public class CreateCodeTopView extends AnchorPane {

    CreateCodeView createCodeView;
    Label templateLabel = new Label("请选择一个模板");
    ComboBox<Template> templateListCombo = new ComboBox();
    Button updateTemplateButton = new Button("修改模板");
    Label createWayLabel = new Label("请选择一个生成方式");
    ToggleGroup schemeGroup = new ToggleGroup();
    RadioButton outDiskRadio = new RadioButton("生成到硬盘");
    RadioButton outViewRadio = new RadioButton("生成到页面");
    Button createCodeButton = new Button("生成代码");
    Label outPathLabel = new Label("请选择输出路径");
    TextField outPathField = new TextField();
    Button selectPathButton = new Button("选择路径");
    Label filePrefixLabel = new Label("文件前缀");
    TextField filePrefixField = new TextField();

    public CreateCodeTopView(CreateCodeView createCodeView){
        this.createCodeView = createCodeView;
        initUI();
        initEvent();

    }



    private void initUI() {

        AnchorPane.setLeftAnchor(templateLabel,10.0);
        AnchorPane.setTopAnchor(templateLabel,10.0);
        AnchorPane.setRightAnchor(templateLabel,400.0);

        AnchorPane.setLeftAnchor(templateListCombo,200.0);
        AnchorPane.setTopAnchor(templateListCombo,10.0);
        AnchorPane.setRightAnchor(templateListCombo,80.0);

        AnchorPane.setTopAnchor(updateTemplateButton,10.0);
        AnchorPane.setRightAnchor(updateTemplateButton,10.0);

        this.getChildren().addAll(templateLabel, templateListCombo,updateTemplateButton);

        AnchorPane.setLeftAnchor(createWayLabel,10.0);
        AnchorPane.setTopAnchor(createWayLabel,50.0);
        AnchorPane.setRightAnchor(createWayLabel,400.0);


        outDiskRadio.setToggleGroup(schemeGroup);
        AnchorPane.setLeftAnchor(outDiskRadio,200.0);
        AnchorPane.setTopAnchor(outDiskRadio,50.0);
        AnchorPane.setRightAnchor(outDiskRadio,307.0);

        outViewRadio.setToggleGroup(schemeGroup);
        outViewRadio.setSelected(true);
        AnchorPane.setLeftAnchor(outViewRadio,350.0);
        AnchorPane.setTopAnchor(outViewRadio,50.0);

        this.getChildren().addAll(createWayLabel, outDiskRadio, outViewRadio);

        outPathLabel.setManaged(false);
        AnchorPane.setLeftAnchor(outPathLabel,10.0);
        AnchorPane.setTopAnchor(outPathLabel,90.0);
        AnchorPane.setRightAnchor(outPathLabel,400.0);

        outPathField.setManaged(false);
//        outPathField.setPromptText("点击这里选择路径");
        AnchorPane.setLeftAnchor(outPathField,200.0);
        AnchorPane.setTopAnchor(outPathField,90.0);
        AnchorPane.setRightAnchor(outPathField,80.0);

        selectPathButton.setManaged(false);
//        AnchorPane.setLeftAnchor(selectPathButton,600.0);
        AnchorPane.setTopAnchor(selectPathButton,90.0);
        AnchorPane.setRightAnchor(selectPathButton,10.0);

        this.getChildren().addAll(outPathLabel, outPathField,selectPathButton);

        filePrefixLabel.setManaged(false);
        AnchorPane.setLeftAnchor(filePrefixLabel,10.0);
        AnchorPane.setTopAnchor(filePrefixLabel,130.0);
        AnchorPane.setRightAnchor(filePrefixLabel,400.0);

        filePrefixField.setManaged(false);
//        outPathField.setPromptText("点击这里选择路径");
        AnchorPane.setLeftAnchor(filePrefixField,200.0);
        AnchorPane.setTopAnchor(filePrefixField,130.0);
        AnchorPane.setRightAnchor(filePrefixField,10.0);

        this.getChildren().addAll(filePrefixLabel, filePrefixField);

        BorderPane.setMargin(this,new Insets(10.0));

    }

    private void initEvent() {
        updateTemplateButton.setOnAction(event -> showUpdateTemplateView());
        selectPathButton.setOnMouseClicked(event -> showFileDirSelect());
        outDiskRadio.setOnAction(event -> showOutPath());
        outViewRadio.setOnAction(event -> hiddenOutPath());
        templateListCombo.setOnAction(event -> queryTempDataTo());

    }

    private void showUpdateTemplateView() {
        SingleSelectionModel<Template> selectionModel = templateListCombo.getSelectionModel();
        Template template = selectionModel.getSelectedItem();
        ObservableMap<String, String> templateObMap = FXCollections.observableHashMap();
        templateObMap.put("TEMP_ID",template.getId());
        templateObMap.put("TEMP_NAME",template.getName());
        createCodeView.getCodeBuilderView().getDiyTemplateView().showUpdateTemplateView(templateObMap);
    }


    public void showFileDirSelect(){
        Stage primaryStage = createCodeView.getCodeBuilderView().getMainView().getPrimaryStage();
        DirectoryChooser dirChiooser = new DirectoryChooser();
        String cwd = System.getProperty("user.dir");
        File file = new File(cwd);
        dirChiooser.setInitialDirectory(file);
        File chosenDir = dirChiooser.showDialog(primaryStage);
        //如果选择了磁盘的某个路径,则把值赋给路径的输入框
        if (chosenDir != null) {
            String absolutePath = chosenDir.getAbsolutePath();
            outPathField.setText(absolutePath);
        }
    }
    private void showOutPath() {

        boolean show = true;

        outPathLabel.setManaged(show);
        outPathField.setManaged(show);
        selectPathButton.setManaged(show);

        filePrefixLabel.setManaged(show);
        filePrefixField.setManaged(show);


    }
    private void hiddenOutPath() {

        boolean show = false;

        outPathLabel.setManaged(show);
        outPathField.setManaged(show);
        selectPathButton.setManaged(show);

        filePrefixLabel.setManaged(show);
        filePrefixField.setManaged(show);
    }

    private void queryTempDataTo() {

        Template template = templateListCombo.getSelectionModel().getSelectedItem();
        if(template!=null){
            String templateId = template.getId();
            ObservableList<ObservableMap<String, String>> templateData = DaoFactory.getTemplateDataDao().query(templateId);
            List<String> fdataList = new ArrayList<>();
            for(int i=0;i<templateData.size();i++){
                ObservableMap<String, String> templateDataItem = templateData.get(i);
                String fData = templateDataItem.get("F_DATA");
                fdataList.add(fData);
            }

            List<String> markList = RegexUtil.decodeTempDataFdata(fdataList);

            createCodeView.getCreateCodeCenterView().getParamPane().getChildren().clear();

            Double topAnchor = 10.0;

            for(int i=0;i<markList.size();i++){

                String mark = markList.get(i);

                Label paramLabel = new Label(mark);
                paramLabel.getStyleClass().add("paramLabelCss");
                AnchorPane.setLeftAnchor(paramLabel,10.0);
                AnchorPane.setTopAnchor(paramLabel,topAnchor);
                AnchorPane.setRightAnchor(paramLabel,400.0);

                TextField paramField = new TextField();
                paramField.getStyleClass().add("paramFieldCss");
                paramField.setPromptText("请输入需要替换的值");
                AnchorPane.setLeftAnchor(paramField,200.0);
                AnchorPane.setTopAnchor(paramField,topAnchor);
                AnchorPane.setRightAnchor(paramField,10.0);


                createCodeView.getCreateCodeCenterView().getParamPane().getChildren().addAll(paramLabel, paramField);

                topAnchor=topAnchor+30.0;
            }
        }


    }


    public ComboBox<Template> getTemplateListCombo() {
        return templateListCombo;
    }

    public Label getTemplateLabel() {
        return templateLabel;
    }

    public Label getCreateWayLabel() {
        return createWayLabel;
    }

    public RadioButton getOutDiskRadio() {
        return outDiskRadio;
    }

    public RadioButton getOutViewRadio() {
        return outViewRadio;
    }

    public Button getCreateCodeButton() {
        return createCodeButton;
    }

//    public Label getCodeFormatLabel() {
//        return codeFormatLabel;
//    }
//
//    public TextField getCodeFormatField() {
//        return codeFormatField;
//    }

    public ToggleGroup getSchemeGroup() {
        return schemeGroup;
    }

    public TextField getOutPathField() {
        return outPathField;
    }

    public TextField getFilePrefixField() {
        return filePrefixField;
    }

    public void setFilePrefixField(TextField filePrefixField) {
        this.filePrefixField = filePrefixField;
    }
}
